package com.tuocent.dagv2.controller;

import com.tuocent.dagv2.common.AjaxResult;
import com.tuocent.dagv2.entity.*;
import com.tuocent.dagv2.service.*;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("basic")
public class BasicController {

    @Autowired
    private IArchivesClassifyService classifyService;
    @Autowired
    private IFieldConfigService fieldConfigService;
    @Autowired
    private IArchivesCodeFormatService codeFormatService;
    @Autowired
    private IArchivesCodeListService archivesCodeListService;

    @Autowired
    private IDictionaryClassService dictionaryClassService;
    @Autowired
    private IArchivesClassService archivesClassService;
    @Autowired
    private IDictionaryService dictionaryService;


    /**
     * 档案门类设置
     **********************************************************************/
    /**
     * 档案门类页面
     */
    @RequestMapping("classifyInfo")
    public String classifyInfo(Model model) {
        return "/basic/archivesClassify";
    }

    /**
     * 获取档案门类信息
     */
    @RequestMapping("getClassifyData")
    @ResponseBody
    public AjaxResult getClassifyData(@RequestBody Map<String, String> data) {
        return classifyService.getClassifyData(data);
    }

    @RequestMapping("setClassifyData")
    public String setClassify(Model model,
                              @RequestParam(value = "id", defaultValue = "") String id) {
        return classifyService.setClassify(model, id);
    }

    @RequestMapping("saveClassifyData")
    @ResponseBody
    public AjaxResult saveClassifyData(@RequestBody ArchivesClassify data) {
        return classifyService.saveClassifyData(data);
    }

    /**
     * 删除档案门类
     */
    @RequestMapping("delClassifyData")
    @ResponseBody
    public AjaxResult delClassifyData(@RequestParam("id") String id) {
        return classifyService.delClassifyData(id);
    }


    /**
     * 获取门类对应的字段配置数据
     */
    @RequestMapping("getFieldConfigList")
    @ResponseBody
    public AjaxResult getFieldConfigList(@RequestBody Map<String, String> params) {
        return fieldConfigService.getFieldConfigList(params);
    }

    @RequestMapping("getShowFieldList")
    @ResponseBody
    public AjaxResult getShowFieldList(@RequestParam(value = "fillingModel") String fillingModel,
                                       @RequestParam(value = "classifyId") String classifyId) {
        return fieldConfigService.getShowFieldList(fillingModel, classifyId);
    }

    @RequestMapping("getFieldConfigData")
    @ResponseBody
    public AjaxResult getFieldConfigData(@RequestParam(value = "classifyId") String classifyId) {
        return fieldConfigService.getFieldConfigData(classifyId);
    }

    /**
     * 设置字段
     *
     * @param id
     * @param classifyId
     * @param fillingModel
     * @param model
     * @return
     */
    @RequestMapping("setFieldConfig")
    public String setFieldConfig(@RequestParam(value = "id", defaultValue = "") String id,
                                 @RequestParam("classifyId") String classifyId,
                                 @RequestParam("fillingModel") String fillingModel,
                                 Model model
    ) {
        return fieldConfigService.setFieldConfig(model, id, classifyId, fillingModel);
    }

    /**
     * 检测选择字段配置
     *
     * @param id
     * @param classifyId
     * @param fillingModel
     * @param value
     * @return
     */
    @RequestMapping("selectField")
    @ResponseBody
    public AjaxResult selectField(@RequestParam("id") String id,
                                  @RequestParam("classifyId") String classifyId,
                                  @RequestParam("fillingModel") String fillingModel,
                                  @RequestParam("value") String value) {
        return fieldConfigService.selectField(classifyId, fillingModel, id, value);
    }

    /***
     * 保存字段设置
     * @param data
     * @return
     */
    @RequestMapping("saveFieldConfig")
    @ResponseBody
    public AjaxResult saveFieldConfig(@RequestBody FieldConfig data) {
        return fieldConfigService.saveFileConfig(data);
    }

    /**
     * 删除字段配置
     *
     * @param classifyId
     * @param id
     * @return
     */
    @RequestMapping("delFieldConfig")
    @ResponseBody
    public AjaxResult delFieldConfig(@RequestParam("classifyId") String classifyId,
                                     @RequestParam("id") String id
    ) {
        return fieldConfigService.delFieldConfig(classifyId, id);
    }

    @RequestMapping("moveMetadataPosition")
    @ResponseBody
    public AjaxResult moveMetadataPosition(@RequestParam("id") String id,
                                           @RequestParam("classifyId") String classifyId,
                                           @RequestParam("fillingModel") String fillingModel,
                                           @RequestParam("action") String action) {
        return fieldConfigService.moveMetadataPosition(id, classifyId, fillingModel, action);

    }


    /***
     * 获取档号格式
     * @param id
     * @param classifyId
     * @return
     */
    @RequestMapping("getArchivesCodeFormat")
    @ResponseBody
    public AjaxResult getArchivesCodeFormat(@RequestParam(value = "id", defaultValue = "") String id,
                                            @RequestParam("classifyId") String classifyId
    ) {
        return codeFormatService.getArchivesCodeFormat(classifyId, id);
    }

    @GetMapping("archivesCodeFormatList")
    public String archivesCodeFormatList(@RequestParam(value = "id", defaultValue = "") String id,
                                         @RequestParam(value = "pid", defaultValue = "") String pid,
                                         @RequestParam("codeModel") String codeModel,
                                         @RequestParam("classifyId") String classifyId,
                                         @RequestParam(value = "children", defaultValue = "") String children,
                                         @RequestParam(value = "parentId", defaultValue = "") String parentId,
                                         Model model) {
        return codeFormatService.archivesCodeFormatList(model, id, pid, codeModel, classifyId, children, parentId);
    }

    @GetMapping("getArchivesFormatCodeList")
    @ResponseBody
    public AjaxResult getArchivesFormatCodeList(@RequestParam(value = "pid") String pid,
                                                @RequestParam(value = "action", defaultValue = "") String action) {
        return archivesCodeListService.getArchivesFormatCodeList(pid, action);
    }

    @RequestMapping("setArchivesCodeFormat")
    public String setArchivesCodeFormat(Model model,
                                        @RequestParam(value = "id", defaultValue = "") String id,
                                        @RequestParam(value = "pid") String pid,
                                        @RequestParam("classifyId") String classifyId,
                                        @RequestParam("children") String children,
                                        @RequestParam("codeModel") String codeModel,
                                        @RequestParam(value = "parentId", defaultValue = "") String parentId

    ) {
        return archivesCodeListService.setArchivesCodeFormat(model, id, pid, classifyId, children, codeModel, parentId);
    }


    @RequestMapping("getDictionaryClassData")
    @ResponseBody
    public AjaxResult getDictionaryClassData(@RequestParam("model") String model) {
        return dictionaryClassService.getDictionaryClassData(model);
    }


    @RequestMapping("saveArchivesCodeFormat")
    @ResponseBody
    public AjaxResult saveArchivesCodeFormat(HttpSession session,
                                             @RequestBody ArchivesCodeList data) {
        return archivesCodeListService.saveArchivesCodeList(session, data);
    }

    @RequestMapping("getParentField")
    @ResponseBody
    public AjaxResult getParentField(@RequestParam("classifyId") String classifyId,
                                     @RequestParam("fillingModel") String fillingModel) {
        return fieldConfigService.getParentField(classifyId, fillingModel);
    }

    /**
     * 删除档号格式（全）
     *
     * @param id
     * @return
     */
    @RequestMapping("delArchivesCodeFormat")
    @ResponseBody
    public AjaxResult delArchivesCodeFormat(@RequestParam("id") String id) {
        return archivesCodeListService.delArchivesCodeFormat(id);
    }

    @RequestMapping("delArchivesCodeFormatList")
    @ResponseBody
    public AjaxResult delArchivesCodeFormatList(HttpSession session,
                                                @RequestParam("id") String id,
                                                @RequestParam("pid") String pid,
                                                @RequestParam("classifyId") String classifyId) {
        return archivesCodeListService.delArchivesCodeFormatList(session, id, classifyId, pid);
    }

    /**
     * 档案分类信息
     *
     * @param model
     * @return
     */
    @RequestMapping("archivesClass")
    public String archivesClass(Model model) {
        return "/basic/archivesClass";
    }

    @RequestMapping("setArchivesClass")
    private String setArchivesClass(Model model,
                                    @RequestParam("pid") String pid,
                                    @RequestParam("tid") String tid,
                                    @RequestParam("id") String id,
                                    @RequestParam("parentId") String parentId) {
        return archivesClassService.setArchivesClass(model, pid, tid, id, parentId);
    }

    @RequestMapping("getArchivesClassData")
    @ResponseBody
    public AjaxResult getArchivesClassData() {
        return archivesClassService.getArchivesClassData();
    }

    @RequestMapping("saveArchivesClass")
    @ResponseBody
    public AjaxResult saveArchivesClass(@RequestBody ArchivesClass data) {
        return archivesClassService.saveArchivesClass(data);
    }

    @RequestMapping("delArchivesClass")
    @ResponseBody
    public AjaxResult delArchivesClass(@RequestParam("id") String id) {
        return archivesClassService.delArchivesClass(id);
    }


    @RequestMapping("dataDictionary")
    public String dataDictionary() {
        return "/basic/dataDictionary";
    }

    @RequestMapping("getDictionaryData")
    @ResponseBody
    public AjaxResult getDictionaryData(@RequestParam("classId") String classId) {
        return dictionaryService.getDictionaryData(classId);
    }

    @RequestMapping("setDictData")
    public String setDictData(@RequestParam("id") String id,
                              @RequestParam("classId") String classId,
                              Model model) {
        return dictionaryService.setDictData(model, id, classId);
    }

    @PostMapping("saveDataDict")
    @ResponseBody
    public AjaxResult saveDataDict(@RequestBody Dictionary data) {
        return dictionaryService.saveDataDict(data);
    }

    //删除分类信息
    @RequestMapping("delDataDict")
    @ResponseBody
    public AjaxResult delDataDict(@RequestBody Map<String, String> data) {
        return dictionaryService.delDataDict(data);
    }

    /**
     * 数据表信息维护
     * *******************************************************************
     **/

    @RequestMapping("tableInfo")
    public String getTableInfo() {
        return "/basic/tableInfo";
    }


}
